package com.nnfe.fyd.feature.inquiryPrice;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.inquiry.RegionName;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.SharePrefUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/5/15.
 * <p/>
 * 描  述：模糊查询，选择小区
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

class SelectCommunityFragment extends ActionBarFragment {
    @Bind(R.id.btn_back_left)
    Button btnBackLeft;
    @Bind(R.id.et_area_name)
    EditText etAreaName;
    @Bind(R.id.listview_region_name)
    ListView listview;
    @Bind(R.id.ll_empty_page)
    LinearLayout llEmptyPage;


    private List<RegionName> regionNameList = new ArrayList<>();
    private SelectComuunityAdapter selectComuunityAdapter;

    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_select_community;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);

        initView();
        return rootView;
    }

    @Override
    protected void initView() {

        etAreaName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString())) {
                    if (regionNameList.size() > 0) {
                        regionNameList.clear();
                    }
                    selectComuunityAdapter.notifyDataSetChanged();
                    return;
                }
                doSearch(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        selectComuunityAdapter = new SelectComuunityAdapter(mContext);
        selectComuunityAdapter.setData(regionNameList);
        listview.setAdapter(selectComuunityAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AutoInquiryPriceActivity activity = (AutoInquiryPriceActivity) getActivity();
                activity.setAreaName(regionNameList.get(position).residentialareaName +
                        "(" + regionNameList.get(position).address + ")");
                activity.setResidentialareaNameAreaName(regionNameList.get(position).residentialareaName);
                activity.setResidentialareaNameAreaName(regionNameList.get(position).address);
                mContext.onBackPressed();
            }
        });

    }


    /**
     * 自动搜索
     *
     * @param filter // 搜索关键字
     */
    private void doSearch(String filter) {
        //DialogUtil.showLoadingDialog(mContext, "请求中");
        String userId = SharePrefUtil.getString(mContext, ShareKey.UserId, "");
        String token = SharePrefUtil.getString(mContext, ShareKey.Token, "");
        Call<HttpResult<List<RegionName>>> call = service.autoSearchCommunity(userId, token, filter);
        call.enqueue(new CallbackImpl<List<RegionName>>() {
            @Override
            public void responseSuccess(Call<HttpResult<List<RegionName>>> call, Response<HttpResult<List<RegionName>>> response) {

                if (response.body().status == 0) {

                    if (response.body().getData() != null && response.body().getData().size() == 0) {
                        llEmptyPage.setVisibility(View.VISIBLE);

                    } else {
                        llEmptyPage.setVisibility(View.GONE);

                    }

                    if (regionNameList.size() > 0) {
                        regionNameList.clear();
                    }
                    regionNameList.addAll(response.body().data);
                    selectComuunityAdapter.notifyDataSetChanged();
                }
            }

        });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        ButterKnife.unbind(this);
    }

    @OnClick({R.id.btn_back_left,R.id.btn_inquiry_manual})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back_left:
                mContext.onBackPressed();

                break;
            case R.id.btn_inquiry_manual:
               startActivity(new Intent(mContext,InquiryPriceActivity.class));
                break;
        }
    }


}
